﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HitchHikersGuideToTheGalaxy.Models
{
    public class MyPagesRepository : HitchHikersGuideToTheGalaxy.Models.IMyPagesRepository
    {
        GalaxyDataContext db = new GalaxyDataContext();

        #region IMyPagesRepository Members

        public IQueryable<TourDest> GetMyPendingTours(string ssn)
        {
            return (from vehicleDriver in db.VehicleDrivers
                    from tour in db.Tours
                    from dest in db.Destinations
                    from dest2 in db.Destinations
                    where vehicleDriver.UserInfoSSN == ssn && vehicleDriver.Id == tour.VehicleDriverId && tour.Time > DateTime.Now
                    orderby tour.Time
                    where tour.DestinationIdTo == dest.Id
                    where tour.DestinationIdFrom == dest2.Id
                    select new TourDest
                    {
                        // tour info
                        IdTour = tour.Id,
                        Time = tour.Time,
                        VehicleDriverId = tour.VehicleDriverId,
                        SmokingAllowed = tour.SmokingAllowed,
                        PetAllowed = tour.PetAllowed,
                        AvailableSeats = tour.AvailableSeats,
                        Distance = tour.Distance,
                        Cost = tour.Cost,
                        Route = tour.Route,
                        Note = tour.Note,

                        //dest to info
                        DestToName = dest.Name,
                        DestToLatitude = dest.Latitude,
                        DestToLongitude = dest.Longitude,

                        //dest from info
                        DestFromName = dest2.Name,
                        DestFromLatitude = dest2.Latitude,
                        DestFromLongitude = dest2.Longitude
                    }).AsQueryable<TourDest>();
        }

        /// <summary>
        /// Get the Passengers in the Tours I'm driving.
        /// </summary>
        /// <returns></returns>
        public IQueryable<MyPages> GetPassengersInMyTours(string ssn)
        {
            return (from vehicleDriver in db.VehicleDrivers
                    from tour in db.Tours
                    from passenger in db.Passengers
                    from dest in db.Destinations
                    from dest2 in db.Destinations
                    where vehicleDriver.UserInfoSSN == ssn && vehicleDriver.Id == tour.VehicleDriverId && passenger.TourId == tour.Id
                    //TODO: Setja time checkið inn eftir test.
                    //where tour.Time > DateTime.Now 
                    //orderby tour.Time
                    where tour.DestinationIdTo == dest.Id
                    where tour.DestinationIdFrom == dest2.Id
                    select new MyPages
                    {   // tour info
                        IdTour = tour.Id,
                        Time = tour.Time,
                        TourTotalCost = tour.Cost,
                        DestToName = dest.Name,
                        DestFromName = dest2.Name,
                        Accepted = passenger.Accepted,
                        PassengerCost = passenger.Cost
                    }).AsQueryable<MyPages>();
        }

        #endregion
    }
}
